package cn.com.czpb.client.module.subscribe.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.app.MyActivitymanager;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.model.subscribe.FocusUserDetailBean;
import cn.com.czpb.client.model.subscribe.FocusUserDetailEntity;
import cn.com.czpb.client.model.subscribe.WriterListEntity;
import cn.com.czpb.client.module.news.adapter.NewsItemListViewAdapterNew;
import cn.com.czpb.client.module.subscribe.view.TopViewSubscribeDetail;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class SubscribeDetailActivity extends MToolBarActivity implements OnRefreshListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.recyclerview_highrise)
    RecyclerView recyclerView;
    @BindView(R.id.swipe_layout)
    SmartRefreshLayout swipe_layout;

    private NewsItemListViewAdapterNew adapter;
    private boolean mFlagRefresh = true;//刷新还是加载
    private String focusId;
    private int page = 1;
    private int pageSize = 15;


    private TopViewSubscribeDetail detailview;
    private String tid;

    @Override
    public void initData() {
        focusId = getIntent().getStringExtra("focusUid");
        tid = getIntent().getStringExtra("tid");
        LogUtils.i("uid-->" + focusId);
        init();
        getFocusUserDetail(focusId);
    }

    private void getFocusUserDetail(String focusId) {
        Map<String, String> map = new HashMap();
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("uid", focusId);
        if (spu.getUser() != null) {
            map.put("nowuid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(this));
        Factory.provideHttpService().focusUserDetail(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<FocusUserDetailEntity>() {
                    @Override
                    public void call(FocusUserDetailEntity entity) {
                        setData(entity.data);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                    }
                });
    }

    public void setData(FocusUserDetailBean bean) {
        tid = bean.getTid();
        detailview = TopViewSubscribeDetail.instance(activity);
        detailview.refreshData(bean);
        adapter.addHeaderView(detailview.getView(),0);
        getNewsList(tid);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_subscribedetail;
    }


    private void init() {
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        adapter = new NewsItemListViewAdapterNew(activity,null,false);
        recyclerView.setAdapter(adapter);
        recyclerView.setFocusable(false);
        swipe_layout.setRefreshHeader(new ClassHeader_other(this));
        swipe_layout.setOnRefreshListener(this);
        adapter.setOnItemChildClickListener(this);
    }

    private void getNewsList(String tid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("tid", tid);
        map.put("Page", page + "");
        map.put("PageSize", pageSize + "");
        Factory.provideHttpService().writerNewsList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<WriterListEntity>() {
                    @Override
                    public void call(WriterListEntity writerListEntity) {
                        setData(writerListEntity.data.getList());
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    private void setData(List<NewsBean> list) {
        if (mFlagRefresh){
            adapter.setNewData(list);
        }else{
            adapter.addData(list);
        }

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        mFlagRefresh = true;
        swipe_layout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipe_layout.finishRefresh();
            }
        }, 2000);
        getNewsList(tid);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        final NewsBean newsBean = (NewsBean) adapter.getItem(position);
        switch (view.getId()) {
            case R.id.newsItem_root:
                ////REFACTOR: 2017/8/18 添加浏览量bug
                MyActivitymanager.pushActivity(activity, newsBean, newsBean.getTid(), "newsitem", newsBean.getRvalue());
                LogUtils.e("rtype--" + newsBean.getRtype());
                break;
        }
    }
}
